package Controller;


import Service.UserOpService;
import models.DailyDispage;
import models.User;
import models.daily;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by 郑凯 on 2016/6/9.
 */
@Controller
public class PageController {

    @Autowired
    private HttpSession session;
    @Autowired
    private UserOpService userOpService;

    @RequestMapping("index")
    public ModelAndView index() {
        if (session.getAttribute("uid") != null) {
            User user = userOpService.getUserByUid((String) session.getAttribute("uid"));
            return new ModelAndView("index", "user", user);
        } else {
            User user = userOpService.getUserByUid("1021021111");
            return new ModelAndView("index", "user", user);
        }
    }

    @RequestMapping("writeBlog")
    public ModelAndView writeBlog(@RequestParam("pid") String pid) {
        ModelAndView modelAndView = new ModelAndView("writeBlog");

        if(session.getAttribute("uid") == null) return new ModelAndView("login","tip","请先登录");
        User user = userOpService.getUserByUid((String) session.getAttribute("uid"));
        modelAndView.addObject("user", user);
        return modelAndView;
    }
    @RequestMapping("editorDaily")
    public ModelAndView editorDaily(String did){

        daily d = userOpService.getDailyByDid(did);
        User u = userOpService.getUserByUid((String) session.getAttribute("uid"));

        ModelAndView modelAndView = new ModelAndView("editorDaily");
        modelAndView.addObject("user",u);
        modelAndView.addObject("daily",d);
        return modelAndView;
    }


    @RequestMapping("login")
    public ModelAndView login() {
        session.invalidate();
        return new ModelAndView("login", "tip", "");
    }

    @RequestMapping("register")
    public ModelAndView register() {
        return new ModelAndView("register","tip","");
    }


    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("list");
        if(session.getAttribute("uid") != null)
        {
            List listDaily = userOpService.getListByUid((String) session.getAttribute("uid"));
            User user = userOpService.getUserByUid((String) session.getAttribute("uid"));
            modelAndView.addObject("listDaily", listDaily);
            modelAndView.addObject("user", user);
            return modelAndView;
        }
        else
        {
            List listDaily = userOpService.getListByUid("1021021111");
            User user = userOpService.getUserByUid("1021021111");
            modelAndView.addObject("listDaily", listDaily);
            modelAndView.addObject("user", user);
            return modelAndView;
        }
    }

    @RequestMapping("info")
    public ModelAndView info(@RequestParam("ld") String did) {

        ModelAndView modelAndView = new ModelAndView("info");
        if (session.getAttribute("uid") != null) {
            User user = userOpService.getUserByUid((String) session.getAttribute("uid"));
            daily d = userOpService.getDailyByDid(did);
            modelAndView.addObject("user", user);
            modelAndView.addObject("daily", d);
            return modelAndView;
        } else {
            User user = userOpService.getUserByUid("1021021111");
            daily d = userOpService.getDailyByDid(did);
            modelAndView.addObject("user", user);
            modelAndView.addObject("daily", d);
            return modelAndView;
        }
    }
    @RequestMapping("Manager")
    public ModelAndView manager(@RequestParam("pid") String pid)
    {
        if(session.getAttribute("uid") != null)
        {
            ModelAndView modelAndView = new ModelAndView("manager");

            String uid = (String) session.getAttribute("uid");
            if(uid == null) return new ModelAndView("login","tip","请先登录");

            int begin = Integer.parseInt(pid) * 15 ;
            DailyDispage dispage = new DailyDispage(uid, begin);
            List listDaily = userOpService.getDailyDispage(dispage);
            dispage.setCountPage(userOpService.getCountDailyDispageByUid(uid));
            User user = userOpService.getUserByUid(uid);

            dispage.setCurPage(Integer.parseInt(pid));

            modelAndView.addObject("listDaily", listDaily);
            modelAndView.addObject("dispage", dispage);
            modelAndView.addObject("user", user);
            return modelAndView;
        }
        else return new ModelAndView("login","tip","请先登录");
    }
    @RequestMapping("setUp")
    public ModelAndView setUp()
    {

        if(session.getAttribute("uid") != null)
        {
            User u =  userOpService.getUserByUid((String) session.getAttribute("uid"));
            return new ModelAndView("setUp","user",u);
        }
        else return new ModelAndView("login","tip","请先登录");
    }
}
